package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc.cloud.cmclink.doc.api.document.emums.BusinessTypeEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SiOperateTypeEnum;
import com.cmc.cloud.cmclink.doc.entity.*;
import com.cmc.cloud.cmclink.doc.mapper.*;
import com.cmc.cloud.cmclink.doc.rpcservice.impl.CustomerRpcServiceImpl;
import com.cmc.cloud.cmclink.doc.service.PatnerSiMaintenanceService;
import com.cmc.cloud.cmclink.doc.vo.blnoblprintlogvo.PrintBlRespVo;
import com.cmes.framework.common.pojo.PageResult;
import com.cmc.cloud.cmclink.doc.vo.partner.PartnerSiMaintenancePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.partner.PartnerSiMaintenancePageRespVO;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 合作方SI维护
 *
 * @author 何果
 */
@Service
@Slf4j
public class PatnerSiMaintenanceServiceImpl implements PatnerSiMaintenanceService {

    @Resource
    private BlnoMapper blnoMapper;
    @Resource
    private BlnoVslvoyMapper blnoVslvoyMapper;
    @Resource
    private BlnoCntrMapper blnoCntrMapper;
    @Resource
    private BlnoScnMapper blnoScnMapper;
    @Resource
    private BlnoCargoMapper blnoCargoMapper;
    @Resource
    private BlnoPaymentMapper blnoPaymentMapper;
    @Resource
    private BlnoFreightMapper blnoFreightMapper;
    @Resource
    private VgmMapper vgmMapper;
    @Resource
    private OperateLogMapper operateLogMapper;
    @Autowired
    private CustomerRpcServiceImpl nameTransferService;

    /**
     * 合作方SI维护分页列表查询
     * @param pageVO 查询条件
     * @return 合作方SI信息列表
     */
    @Override
    public PageResult<PartnerSiMaintenancePageRespVO> qryPartnerSiMaintenancePage(PartnerSiMaintenancePageReqVO pageVO) {

        pageVO.setEndTime(pageVO.getEndTime().plusDays(1));
        IPage<PartnerSiMaintenancePageRespVO> objectPage = Page.of(pageVO.getPageNo(), pageVO.getPageSize());
        IPage<PartnerSiMaintenancePageRespVO> blnoContactRespVoIpage = blnoMapper.selectPartnerSiMaintenancePageList(objectPage, pageVO);
        List<PartnerSiMaintenancePageRespVO> records = blnoContactRespVoIpage.getRecords();
        //订舱人编码转英文名
        if (!CollectionUtils.isEmpty(records)){
            List<String> collect = records.stream().map(PartnerSiMaintenancePageRespVO::getBkpCode).collect(Collectors.toList());
            Map<String, String> companyNameMap = nameTransferService.getCompanyNameMap(collect);
            records.stream().forEach(t -> {
                t.setCompanyNameEn(companyNameMap.get(t.getBkpCode()));
            });
        }
        return new PageResult<PartnerSiMaintenancePageRespVO>(records, blnoContactRespVoIpage.getTotal());

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatchBlnoById(List<Long> ids) {

       if (CollectionUtil.isNotEmpty(ids)){
           List<BlnoDO> blnoDos = blnoMapper.selectBatchIds(ids);
            if (CollectionUtil.isNotEmpty(blnoDos)){
                int i = blnoMapper.deleteBatchIds(ids);
                LambdaQueryWrapperX<BlnoVslvoyDO> blnoVslvoyDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoVslvoyDoLambdaQueryWrapperX.in(BlnoVslvoyDO::getBlnoId,ids);
                int deleteVslvoy = blnoVslvoyMapper.delete(blnoVslvoyDoLambdaQueryWrapperX);
                LambdaQueryWrapperX<BlnoCntrDO> blnoCntrDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoCntrDoLambdaQueryWrapperX.in(BlnoCntrDO::getBlnoId,ids);
                int deleteCntr = blnoCntrMapper.delete(blnoCntrDoLambdaQueryWrapperX);
                LambdaQueryWrapperX<BlnoScnDO> blnoScnDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoScnDoLambdaQueryWrapperX.in(BlnoScnDO::getBlnoId,ids);
                int deleteScn = blnoScnMapper.delete(blnoScnDoLambdaQueryWrapperX);
                LambdaQueryWrapperX<BlnoCargoDO> blnoCargoDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoCargoDoLambdaQueryWrapperX.in(BlnoCargoDO::getBlnoId,ids);
                int deleteCargo = blnoCargoMapper.delete(blnoCargoDoLambdaQueryWrapperX);
                LambdaQueryWrapperX<BlnoPaymentDO> blnoPaymentDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoPaymentDoLambdaQueryWrapperX.in(BlnoPaymentDO::getBlnoId,ids);
                int deletePayment = blnoPaymentMapper.delete(blnoPaymentDoLambdaQueryWrapperX);
                LambdaQueryWrapperX<BlnoFreightDO> blnoFreightDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                blnoFreightDoLambdaQueryWrapperX.in(BlnoFreightDO::getBlnoId,ids);
                int deleteFreight = blnoFreightMapper.delete(blnoFreightDoLambdaQueryWrapperX);

                LambdaQueryWrapperX<VgmDO> vgmDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
                vgmDoLambdaQueryWrapperX.in(VgmDO::getBlNo,blnoDos.stream().map(BlnoDO::getBlNo).collect(Collectors.toList()));
                int deleteVgm = vgmMapper.delete(vgmDoLambdaQueryWrapperX);

                //写入操作日志
                Map<Long, String> stringMap = blnoDos.stream().collect(Collectors.toMap(BlnoDO::getId, BlnoDO::getBookingNo));
                List<OperateLogDO> operateLogDos = new ArrayList<>();
                ids.forEach(n -> {
                    OperateLogDO operateLog = new OperateLogDO();
                    operateLog.setBusinessId(n);
                    operateLog.setBusinessNo(stringMap.get(n));
                    operateLog.setBusinessType(BusinessTypeEnum.SI.getValue());
                    operateLog.setOperateType("SI_Operate");
                    operateLog.setOperateTypeSub(SiOperateTypeEnum.DELETE_SI.getValue());
                    operateLog.setOperateDesc(SiOperateTypeEnum.DELETE_SI.getLabel());
                    operateLogDos.add(operateLog);
                });
                operateLogMapper.insertBatch(operateLogDos);
            }

       }


    }
}
